package com.example.myproject.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.myproject.R;
import com.example.myproject.fragment.adapter.HomePagerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * Created by Administrator on 2018/3/25.
 * <p>
 * 商品列表fragment
 */

public class HomeFragment extends Fragment {


    private ViewPager vp_title_page;
    private LinearLayout ll_page_point;
    private TextView[] pointTextArr;
    private SmartRefreshLayout smasrtRefresh;
    private LinearLayout ll_liu1;
    private LinearLayout ll_liu2;
    private Handler handler = new Handler();
    private Runnable runnable;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, null);

//        Build.VERSION_CODES.LOLLIPOP  系统属性是Build
        vp_title_page = rootView.findViewById(R.id.vp_title_page);
        ll_page_point = rootView.findViewById(R.id.ll_page_point);
        smasrtRefresh = rootView.findViewById(R.id.home_refresh);
        ll_liu1 = rootView.findViewById(R.id.ll_liu1);
        ll_liu2 = rootView.findViewById(R.id.ll_liu2);

        initPage();
        initRefreshLayout();
        return rootView;
    }

    private void initRefreshLayout() {
        runnable = new Runnable() {
            @Override
            public void run() {
                if (ll_liu1.getChildCount() < 12 && ll_liu2.getChildCount() < 12)
                    for (int i = 0; i < 5; i++) {
                        View layContent = LayoutInflater.from(getContext()).inflate(R.layout.home_item_content, null);
                        if (ll_liu1.getChildCount() <= ll_liu2.getChildCount()) {
                            ll_liu1.addView(layContent);
                        } else {
                            ll_liu2.addView(layContent);
                        }
                    }
            }
        };
        smasrtRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        smasrtRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                handler.postDelayed(runnable, 1900);
            }
        });
    }

    private void initPage() {
        HomePagerAdapter homePagerAdp = new HomePagerAdapter(this.getContext());
        vp_title_page.setAdapter(homePagerAdp);
        vp_title_page.setCurrentItem(Integer.MAX_VALUE / 2);

        pointTextArr = new TextView[9];
        for (int i = 0; i < 9; i++) {
            TextView tv = new TextView(this.getContext());
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
            lp.width = 20;
            lp.height = 20;
            lp.setMargins(5, 0, 0, 0);
            tv.setLayoutParams(lp);
            if (i == 0)
                tv.setBackgroundResource(R.drawable.shape_yuan_red);
            else
                tv.setBackgroundResource(R.drawable.shape_yuan_white);
            pointTextArr[i] = tv;
            ll_page_point.addView(tv);
        }

        vp_title_page.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                //由于 position 是 Integer.MAX的，所以不能用  i==position来对比
                int i1 = position % 9;
                for (int i = 0; i < 9; i++) {
                    if (i == i1)
                        pointTextArr[i].setBackgroundResource(R.drawable.shape_yuan_red);
                    else
                        pointTextArr[i].setBackgroundResource(R.drawable.shape_yuan_white);
                }
                super.onPageSelected(position);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //注意，这里只有在show和hide的模式才能用，replace的模式下不可使用，
        pointTextArr = null;
        ll_page_point = null;
        vp_title_page = null;

        if (runnable != null)
            handler.removeCallbacks(runnable);
        handler = null;
    }
}
